package com.mgr.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mgr.model.Transactions;
import com.mgr.service.AccountService;
import com.mgr.service.OTPService;
import com.mgr.service.SmsService;
import com.mgr.service.TransactionService;
import com.mgr.service.WebServiceBank;

@Controller
public class ExternalTransferController3 {

	public static int countOTP = 0;
	@Autowired
	private WebServiceBank webServiceBank;
	@Autowired
	private AccountService accountService;
	@Autowired
	private TransactionService transactionService;

	@Autowired
	private SmsService smsService;

	@Autowired
	private OTPService otpService;

	private ResourceBundle rb = ResourceBundle
			.getBundle("com.mgr.messages.errorMessage");

	@RequestMapping("/externalTransfer3")
	public ModelAndView processexternalTransfer2(HttpSession session) {
		ModelAndView modeView = new ModelAndView();
		String id = (String) session.getAttribute("user");
		if (id == null || id == "") {
			modeView.setViewName("login");
		} else if (session.getAttribute("step2") == null) {
			modeView.setViewName("redirect:externalTransfer2");
		} else{
			modeView.addObject(session.getAttribute("balance"));
			modeView.addObject("trans", session.getAttribute("trans"));
			modeView.setViewName("externalTransfer3.tile");
		}
		return modeView;
	}

	@RequestMapping("/ExternalTrans3")
	public ModelAndView doAction3(HttpSession session,
			HttpServletRequest request) {
		String codeOtp = (String) session.getAttribute("codeOTP");
		Transactions trans = (Transactions) session.getAttribute("trans");
		ModelAndView modeView = new ModelAndView();
		String id = (String) session.getAttribute("user");
		if (id == null || id == "") {
			modeView.setViewName("login");
		} else if (trans == null) {
			modeView.setViewName("redirect:externalTransfer1");
		} else if (session.getAttribute("step2") == null) {
			modeView.setViewName("redirect:externalTransfer2");
		} else {
			trans.setTransDate(new Date());
			session.setAttribute("trans", trans);
			int accountSender = trans.getAccId1();
			int amountOfMoney = (int) trans.getAmount();
			String codeOTP = request.getParameter("codetOTP");
			if (codeOtp.equals(codeOTP)) {
				accountService.updateBalance(accountSender, -amountOfMoney);
				session.setAttribute("time", new Date());
				if (webServiceBank.isExistAccAndUpdateBalance(trans.getAccId2(), amountOfMoney)) {
					session.setAttribute("transState", "Success");
				} else {
					accountService.updateBalance(accountSender, amountOfMoney);
					transactionService.addtransaction(trans);
					session.setAttribute("transState", "Not Success");
				}
				session.setAttribute("step3", true);
				modeView.setViewName("redirect:externalTransferResult");
				session.setAttribute("balance",accountService.getAccount(accountSender).getBalance());
			} else {
				countOTP++;
				modeView.addObject("err", rb.getString("errOTP"));
				modeView.setViewName("externalTransfer3.tile");
				if(countOTP > 2) {
					modeView.addObject("err", rb.getString("errOverOTP"));
					modeView.setViewName("welcome.tile");
				}
			}
		}
		return modeView;
	}

	@RequestMapping("externalTransferResult")
	public ModelAndView processexternalTransferResult(HttpSession session) {
		ModelAndView modeView = new ModelAndView();
		String id = (String) session.getAttribute("user");
		if (id == null || id == "") {
			modeView.setViewName("login");
		} else if(session.getAttribute("step3") == null){
			modeView.setViewName("redirect:externalTransfer3");
		}else {
			modeView.addObject(session.getAttribute("balance"));
			modeView.addObject("trans", session.getAttribute("trans"));
			modeView.setViewName("externalTransferResult.tile");
		}
		return modeView;
	}

	@RequestMapping("exterTransPrint")
	public ModelAndView doPrintExterTrans(HttpSession session) {
		ArrayList<String[]> transInfo = new ArrayList<String[]>();
		Transactions trans = (Transactions) session.getAttribute("trans");
		String[] temp = new String[2];
		temp[0] = "TITLE";
		temp[1] = "EXTERNAL TRANSFER MONEY";
		transInfo.add(temp);
		temp = new String[2];
		temp[0] = "ACCOUNT SENDER";
		temp[1] = trans.getAccId1() + "";
		transInfo.add(temp);
		temp = new String[2];
		temp[0] = "BALANCE";
		int accId = trans.getAccId1();
		int balance = accountService.getAccount(accId).getBalance();
		temp[1] = balance + "VND";
		transInfo.add(temp);
		temp = new String[2];
		temp[0] = "ACCOUNT RECIEVE";
		temp[1] = trans.getAccId2() + "";
		transInfo.add(temp);
		temp = new String[2];
		temp[0] = "AMOUNT";
		temp[1] = trans.getAmount() + " VND";
		transInfo.add(temp);
		temp = new String[2];
		temp[0] = "TIME";
		temp[1] = trans.getTransDate().toString();
		transInfo.add(temp);
		temp = new String[2];
		temp[0] = "TRANSFER STATE";
		temp[1] = session.getAttribute("transState").toString().toUpperCase();
		transInfo.add(temp);
		return new ModelAndView("pdfView", "printData", transInfo);
	}

	@RequestMapping("sendCodeAgain")
	public String doSendCode(HttpSession session) {
		String codeOTP = smsService.createOTP();
		session.setAttribute("codeOTP", codeOTP);
		int custId = Integer.parseInt((String) session.getAttribute("user"));
		otpService.sendEmail(custId, codeOTP);
		return "externalTransfer3.tile";
	}

}
